// pages/accination-allPlan/accination-allPlan.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    planList:[],
    babyId:'',
    toView:'',
    scrollId:'',
    height:0,
    allHeight:0,
    scrollHeight:0,
    week: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      babyId: options.babyId,
      scrollId: options.planId
    })
    let title = options.name+"的接种计划"
    wx.setNavigationBarTitle({
      title: title
    })
    this.getAllPlan()
    
  },
getHeight(){
  let that=this
  //不滑动页面的高度
  wx.getSystemInfo({
    success: function (res, rect) {
      console.log(res)
      that.setData({
        allHeight: res.windowHeight
      })
    }
  })
  that.setData({
    scrollHeight: this.data.allHeight
  })
  console.log(this.data.scrollHeight)
},

getAllPlan(){
  utils.HttpRequst(true, '/baby-cloud-base/mobileVaccinePlan/listAll', 'post', { babyId: this.data.babyId},res=>{
    if (res.data.code == 200) {
      var data = res.data.data
      let indexs = ''
      data.forEach((item, index) => {
        item.viewIndex = "idx-" + index
        if (this.data.scrollId == item.id) {
          indexs = item.viewIndex
        }
        item.planDate = item.planTime.split(" ")[0];
        var currentDate = new Date().getTime();
        var newPlanDate = new Date(item.planTime.replace(/-/g,"/")).getTime();
        item.timeCha = parseInt((currentDate - newPlanDate) / (1000 * 60 * 60 * 24));
        let d = item.differenceDays;
        item.planDay = d 
        let arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        item.week = arr[new Date(item.planDate).getDay()];
        // if (d == 0) {
        //   item.planDay = '今天'
        // } else if (d == 1) {
        //   item.planDay = '明天'
        // } else {
        //   item.planDay = d + '天后'
        // }
      })
      console.log(data)
      this.setData({
        planList: data,
      })
      setTimeout(() => {
        this.setData({
          toView: indexs
        })
      }, 200)
    }
  })
  // request({
  //   url: '/baby-cloud-base/mobileVaccinePlan/listAll',
  //   method: 'post',
  //   data: {
  //     babyId:this.data.babyId
  //   }
  // }).then(res => {
  //   if (res.data.code == 200) {
  //     var data = res.data.data
  //     let indexs = ''
  //     data.forEach((item,index)=>{
  //       item.viewIndex="idx-"+index
  //       if (this.data.scrollId==item.id){
  //         indexs = item.viewIndex
  //       }
  //       item.planDate = item.planTime.split(" ")[0];
  //       let d = item.differenceDays;
  //         if (d == 0) {
  //           item.planDay = '今天'
  //         } else if (d == 1) {
  //           item.planDay = '明天'
  //         } else {
  //           item.planDay = d + '天后'
  //         }
  //     })
  //     this.setData({
  //       planList: data,
  //     })
  //     setTimeout(()=>{
  //       this.setData({
  //         toView: indexs
  //       })
  //     },200)
  //     }
  // })
},
  // 进入接种详情页
  planDetail: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/accination-planDetail/accination-planDetail?id=' + id+'&babyId='+this.data.babyId
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllPlan()
    this.getHeight()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})